﻿#region USING
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;
using System.Reflection;
using System.Diagnostics;
#endregion

public class Updater 
{
    static bool loaded = false;
    static Form client;
    static Exception except;
    string file_list = "Client.dll";
    string url = "http://google-yandex-ranking.googlecode.com/files/";
    const string for_google = "asdasdua6sST&dDFasdiadiasdasdiasdafkhGGalsifgasf6yBJKaspgihags"; //эта хрень чтоб гугл закачивал обновлённую версию

    int jopa = 1;
    public Updater()
    {
        try
        {
            FileInfo info;
#if test
            byte[]rez; 
            string[] files = file_list.Split(' ');
            foreach (string file in files)
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url + file);
                req.Timeout = 2000;
                HttpWebResponse  response = (HttpWebResponse)req.GetResponse();
                DateTime server_dt = response.LastModified;
                DateTime local_dt = DateTime.Now;
                if (File.Exists(file)) //есть ранее загруженная версия
                {
                    info = new FileInfo(file);
                    local_dt = info.LastWriteTime;
                }
                if (local_dt != server_dt)
                {
                    using (Stream stream = response.GetResponseStream())
                    using (BinaryReader br = new BinaryReader(stream))
                    {
                        rez = br.ReadBytes((int)response.ContentLength);
                        File.WriteAllBytes(file, rez);
                    }
                }
            }
#else
            loaded = true;
#endif

      
          // AppDomain.CurrentDomain.SetShadowCopyFiles();
           AppDomain newDomain = AppDomain.CreateDomain("newDomain");

           byte[] rawAssembly = File.ReadAllBytes("Client.dll");
           Assembly assembly = newDomain.Load(rawAssembly, null);
           client = (Form)assembly.CreateInstance("Client");
           assembly = null;
           AppDomain.Unload(newDomain);
           newDomain = null;
           loaded = true;
        }
        catch (Exception e)
        {
            except = e;
        }
    }
    [STAThread]
    static void Main(string[] url)
    {
        Updater updater = new Updater();
        Application.EnableVisualStyles();
        if (loaded)
        {
            Application.Run(client);
        }
        else
            Application.Run(new Form1(except));
        
       
    }
}

